//
//  HCStatusListViewModel.swift
//  Hweibome
//
//  Created by 何川 on 15/12/10.
//  Copyright © 2015年 hechuan. All rights reserved.
//

import UIKit

class HCStatusListViewModel: NSObject {

//    var statusList: [HCStatus]?
    var statusList: [HCStatusViewModel]?
    
    func loadData(isPullUp: Bool, complete:(isSuccess:Bool,count: Int)->()){
        
        var maxId = isPullUp == true ? (statusList?.last?.status?.id ?? 0) : 0
        
        if maxId > 0 {
            maxId -= 1
        }
        
        let sinceId = isPullUp == true ? 0 : (statusList?.first?.status?.id ?? 0)
        HCNetworkTools.shareTools.loadStatus(HCUserAccountViewModel.sharedAccount.accessToken!,sinceId: sinceId , maxid: maxId){ (response, error) -> () in
            if error != nil {
                printLog("\(error)")
                complete(isSuccess: false , count: 0)
                return
            }
            
            guard let result = response as? [String:AnyObject] else {
                printLog("没有statuses")
                return
            }
            
            
            guard let statusDicts = result["statuses"] as? [[String:AnyObject]] else {
                printLog("没有statuses")
                return
            }
            var tempArray = [HCStatusViewModel]()
            
            for value in statusDicts {
                let status = HCStatus(dict: value)
                tempArray.append(HCStatusViewModel(status: status))
            }
            
            printLog("加载回来:\(tempArray.count)")
            if self.statusList == nil {
                self.statusList = [HCStatusViewModel]()
            }
            
            if isPullUp {
                self.statusList = self.statusList! + tempArray
            }else {
                self.statusList = tempArray + self.statusList!
            }
            
            complete(isSuccess: true, count: tempArray.count)
            
        }
    }
    
}
